﻿using System.Collections.Generic;
using System.ComponentModel;
using cndp.Model;
using cndp.Model.IModel;
using cndp.View;

namespace cndp.Presenter
{
    public class PresenterFormMain
    {

        #region Constructor

        public PresenterFormMain(IFormMain view, ContestList cl, ContestList OldCl)
        {
            ModelContestList = cl;
            View = view;
            ModelOldContestList = OldCl;
            View.SetContestList(cl);
            View.EventNewContest += NewContest;
            View.EventDeleteContest += DeleteContest;
            View.EventGetNumberOfDivers += GetNumberOfDivers;
            View.EventGetNumberOfJudges += GetNumberofJudges;
            View.EventGetContest += GetContest;
            View.EventAddDiverToRegister += AddDiverToRegister;
            View.EventAddJudgeToRegister += AddJudgeToRegister;
            View.EventGetDiverRegister += GetDiverRegister;
            View.EventGetJudgeRegister += GetJudgeRegister;
            View.EventSaveContestToXmlFile += SaveContestToXmlFile;
            View.EventAddEventToContest += AddEventToContest;
            View.EventGetEventList += GetEventList;
            View.EventDeleteEventFromContest += DeleteEventFromContest;
            View.EventGetContestList += GetContestList;
            View.EventGetEvent += GetEvent;
            View.EventGetSignedDiversToEvent += GetSignedDiversToEvent;
            View.EventReadContestListFromXmlFile += ReadContestListFromXmlFile;
            View.EventSaveContestListFromXmlFile += SaveContestListToXmlFile;
            View.EventReadContestFromXmlFile += ReadContestFromXmlFile;
            View.EventReadOldContestListFromXmlFile += ReadOldContestListFromXmlFile;
            View.EventSaveOldContestListToXmlFile += SaveOldContestListToXmlFile;
            View.EventSaveNewOldContest += SaveNewOldContest;
            View.EventGetOldContestList += GetOldContestList;
            View.EventGetOldDiverRegister += GetOldDiverRegister;
            View.EventGetOldJudgeRegister += GetOldJudgeRegister;
            View.EventGetOldNumberOfDivers += GetOldNumberOfDivers;
            View.EventGetOldNumberOfJudges += GetOldNumberOfJudges;
            View.EventGetOldContest += GetOldContest;
            View.EventGetOldEventList += GetOldEventList;
            View.EventGetOldEvent += GetOldEvent;
        }


        #endregion

        #region Methods

        private int GetNumberofJudges(int index)
        {
            return ModelContestList.GetNumberOfJudgesForSpecificContest(index);
        }

        private int GetOldNumberOfJudges(int index)
        {
            return ModelOldContestList.GetNumberOfJudgesForSpecificContest(index);
        }
        private int GetOldNumberOfDivers(int index)
        {
            return ModelOldContestList.GetNumberOfDiversForSpecificContest(index);
        }
        private int GetNumberOfDivers(int index)
        {
            return ModelContestList.GetNumberOfDiversForSpecificContest(index);
        }

        public void NewContest(string name, int id, string location, string startTime, string finishTime,
            string deadlineDate,
            string contestAdministrator)
        {
            var c = new Contest(name, id, location, startTime, finishTime, deadlineDate, contestAdministrator);
            ModelContestList.AddContest(c);
        }

        public void SaveNewOldContest(Contest c)
        {
            ModelOldContestList.AddContest(c);
        }
        public void DeleteContest(int id)
        {
            ModelContestList.DeleteContestByIndex(id);
        }

        public Contest GetContest(int index)
        {
            return ModelContestList.GetContest(index);
        }

        public Contest GetOldContest(int index)
        {
            return ModelOldContestList.GetContest(index);
        }
        public void AddDiverToRegister(string name, string gender, string personalcodenumber, string nationality,
            string club, int index)
        {
            var d = new Diver(name, gender, personalcodenumber, nationality, club);
            ModelContestList.GetContest(index).DiverRegister.Add(d);
        }

        public void AddJudgeToRegister(string name, string gender, string personalcodenumber, string nationality,
            string affilliate, int index)
        {
            var j = new Judge(name, gender, personalcodenumber, nationality, affilliate);
            ModelContestList.GetContest(index).JudgeRegister.Add(j);
        }

        public BindingList<Diver> GetDiverRegister(int index)
        {
            return ModelContestList.GetContest(index).DiverRegister;
        }

        public BindingList<Judge> GetJudgeRegister(int index)
        {
            return ModelContestList.GetContest(index).JudgeRegister;
        }

        public BindingList<Judge> GetOldJudgeRegister(int index)
        {
            return ModelOldContestList.GetContest(index).JudgeRegister;
        }

        public BindingList<Diver> GetOldDiverRegister(int index)
        {
            return ModelOldContestList.GetContest(index).DiverRegister;
        }

        public BindingList<Event> GetEventList(int index)
        {
            return ModelContestList.GetContest(index).EventList;
        }
        public BindingList<Event> GetOldEventList(int index)
        {
            return ModelOldContestList.GetContest(index).EventList;
        }
        public void SaveContestToXmlFile(string fileName, int index)
        {
           ModelContestList.GetContest(index).SaveContestToXmlFile(fileName);
        }

        public Contest ReadContestFromXmlFile(string fileName)
        {
            return ModelContestList.ReadContestFromXmlFile(fileName);
        }
        public void AddEventToContest(int id, string gender, string bracket, double jumpHeight, int rounds, int maxDivers,
            int maxJudges,
            string startTime, string finishTime, bool couple, int index)
        {
            ModelContestList.GetContest(index).AddEvent(id,gender,bracket,jumpHeight,rounds,maxDivers,maxJudges,startTime,finishTime,couple);
        }

        public void DeleteEventFromContest(int rowOfContest, int rowOfEvent)
        {
            var eventId = ModelContestList.GetContest(rowOfContest).EventList[rowOfEvent].Id;
            ModelContestList.GetContest(rowOfContest).DeleteEvent(eventId);
        }

        public ContestList GetContestList()
        {
            return ModelContestList.GetContestList();
        }

        public ContestList GetOldContestList()
        {
            return ModelOldContestList.GetContestList();
        }
        public Event GetEvent(int rowOfContest, int rowOfEvent)
        {
            return ModelContestList.GetContest(rowOfContest).EventList[rowOfEvent];
        }
        public Event GetOldEvent(int c, int e)
        {
            return ModelOldContestList.GetContest(c).EventList[e];
        }
        public BindingList<DiverWithJumpCodes> GetSignedDiversToEvent(int rowOfContest, int rowOfEvent)
        {
            return ModelContestList.GetContest(rowOfContest).EventList[rowOfEvent].DiverList;
        }

        public void SaveContestListToXmlFile(string fileName)
        {
            ModelContestList.SaveContestListToXmlFile(fileName);
        }

        public void ReadContestListFromXmlFile(string fileName)
        {
            ModelContestList.ReadContestListFromXmlFile(fileName);
        }
        public void ReadOldContestListFromXmlFile(string filename)
        {
            ModelOldContestList.ReadContestListFromXmlFile(filename);
        }
        public void SaveOldContestListToXmlFile(string filename)
        {
            ModelOldContestList.SaveContestListToXmlFile(filename);
        }
        
        #endregion

        #region Properties

        public IFormMain View { get; set; }
        public IContestList ModelContestList { get; set; }
        public IContestList ModelOldContestList { get; set; }

        #endregion
    }
}